package cn.dnui_wnx725.dnuifood_16110100725.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.squareup.picasso.Picasso;

import java.util.List;

import cn.dnui_wnx725.dnuifood_16110100725.R;
import cn.dnui_wnx725.dnuifood_16110100725.Server;
import cn.dnui_wnx725.dnuifood_16110100725.bean.CollectBean;
import cn.dnui_wnx725.dnuifood_16110100725.bean.FoodBean;
import cn.dnui_wnx725.dnuifood_16110100725.listener.TListener;
import cn.dnui_wnx725.dnuifood_16110100725.model.CollectShopModel;


public class FoodByShopAdapter extends BaseAdapter<FoodBean> {
    OnItemClickListener mOnItemClickListener;

    public FoodByShopAdapter(Context context, List<FoodBean> items, int layoutResource) {
        super(context, items, layoutResource);
    }

    public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
        final int mposition=position;
        final FoodBean bean=items.get(position);
        if(bean==null) return;
        final myViewHolder holder1 = (myViewHolder)holder;
        holder1.textView.setText(bean.getFoodname());
        holder1.textView2.setText("介绍: "+bean.getIntro());
        holder1.textView3.setText("价格: ￥"+bean.getPrice());
        Picasso.with(context).load(Server.BASEIMG+ bean.getPic()).placeholder(R.drawable.f1).into(holder1.imageView);
        TListener<CollectBean> tListener = new TListener<CollectBean>() {
            @Override
            public void onResponse(CollectBean collectBean) {
                if (collectBean.getCollected().equals("1")){
                    holder1.imageButton.setImageResource(R.drawable.ic_collected);
                }
                else {
                    holder1.imageButton.setImageResource(R.drawable.ic_collect);
                }
            }

            @Override
            public void onFail(String msg) {
            }
        };

        CollectShopModel collectShopModel = new CollectShopModel();
        collectShopModel.judge(getUserID(),bean.getFood_id(),"1",tListener);


        if(mOnItemClickListener!=null){
            holder.itemView.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View view) {
                    mOnItemClickListener.onItemClick(view,mposition);
                }
            });
        }
    }

    public interface OnItemClickListener{
        void onItemClick(View view,int position);
    }
    public void setmOnItemClickListener(OnItemClickListener listener){
        mOnItemClickListener=listener;
    }
}
